package com.xbongbong.crm.help;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.domain.entity.PaasListGroupEntity;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.parent.help.PublicGroupHelp;
import com.xbongbong.parent.pojo.GroupConditionPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.pojo.list.ConditionListPojo;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.statistic.result.pojo.ElasticAggsPojo;
import com.xbongbong.saas.model.IndexTypeModel;
import com.xbongbong.util.AddressUtil;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;

/**
 * 公海池
 * @author 吴峰
 * @date 2021/12/24 14:07
 */
@Component
public class PublicHelp {

    @Resource
    private PublicGroupHelp publicGroupHelp;
    @Resource
    private EsHelper esHelper;
    @Resource
    private IndexTypeModel indexTypeModel;

    /**
     * 公海校验
     * @param userVO
     * @param dataId
     * @param businessType
     * @return false 无权限
     * @throws XbbException
     */
    public Boolean publicCustomerCheck(UserVO userVO, Long dataId, Long formId, Integer businessType) throws XbbException {
        String corpid = userVO.getCorpid();
        ConditionListPojo conditionListPojo = new ConditionListPojo(corpid, formId, businessType, null, null, new ArrayList<>(), new ArrayList<>(), userVO, null, null, null, null, null);
        GroupConditionPoJo groupConditionPoJo = publicGroupHelp.getPublicGroup(conditionListPojo);
        PaasListGroupEntity paasListGroupEntity = groupConditionPoJo.getPaasListGroupEntity();
        // 地址查询条件封装
        AddressUtil.addressSearchShouldCondition(groupConditionPoJo.getShouldConditionList());
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termQuery(FieldTypeEnum.DATAID.getAlias(), dataId));
        boolQueryBuilder.filter(termQuery("del", DelEnum.NORMAL.getDel()));
        List<ConditionsEntityExt> listGroupConditionList = JSONArray.parseArray(paasListGroupEntity.getJsonData(), ConditionsEntityExt.class);
        if (!Objects.isNull(listGroupConditionList) && !listGroupConditionList.isEmpty()){
            EsUtil.parseCondition(boolQueryBuilder,listGroupConditionList,"",corpid,businessType);
        }
        if (!Objects.isNull(conditionListPojo.getShouldConditionList()) && !conditionListPojo.getShouldConditionList().isEmpty()){
            EsUtil.parseShouldCondition(boolQueryBuilder,conditionListPojo.getShouldConditionList(),"",corpid,businessType);
        }
        IndexTypeEnum indexTypeEnum = indexTypeModel.getIndexType(userVO.getCorpid(), businessType, SaasMarkEnum.SAAS.getCode());
        ElasticAggsPojo elasticAggsPojo = esHelper.elasticAggs(indexTypeEnum, boolQueryBuilder, null);
        return StringUtil.toInt(elasticAggsPojo.getTotal(), 0) > 0;
    }
}
